// (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
// Use is subject to license terms.

package demo.basic.hp.registry;

import org.systinet.uddi.InvalidParameterException;
import org.systinet.uddi.client.v3.UDDIException;
import org.systinet.uddi.client.v3.UDDISecurityStub;
import org.systinet.uddi.client.v3.UDDI_Security_PortType;
import org.systinet.uddi.client.v3.struct.AuthToken;
import org.systinet.uddi.client.v3.struct.Get_authToken;
import org.systinet.uddi.client.wsdl2uddi.v3.Wsdl2uddiApi;
import org.systinet.uddi.client.wsdl2uddi.v3.Wsdl2uddiStub;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.Publish_wsdl;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.Wsdl;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.WsdlDetail;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.WsdlMapping;

import javax.xml.soap.SOAPException;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

public class WSDL2UDDIPublishing {

    public static final String URL_WSDL_PUBLISHING = "http://localhost:8089/uddi/wsdl2uddi";
    public static final String URL_SECURITY = "http://localhost:8089/uddi/security";

    private static final String USER_NAME = "demo_john";
    private static final String USER_PASSWORD = "demo_john";
    private Wsdl2uddiApi publishing = null;
    private UDDI_Security_PortType security = null;
    private AuthToken authToken = null;

    public WSDL2UDDIPublishing() {
    	try {
			publishing = Wsdl2uddiStub.getInstance(URL_WSDL_PUBLISHING);
			security = UDDISecurityStub.getInstance(URL_SECURITY);
			authToken = security.get_authToken(new Get_authToken(USER_NAME, USER_PASSWORD));
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (UDDIException e) {
			e.printStackTrace();
		} catch(InvalidParameterException e) {
			e.printStackTrace();
		}
    }

    public void publishWSDL2Registry(String wsdlLocation) {
    	String businessKey = "uddi:systinet.com:demo:basic";
    	System.out.println("Publishing business entity, service, binding and tModel instances...");
    	try {
    		WsdlMapping wsdlMapping = new WsdlMapping();
    		wsdlMapping.setBusinessKey(businessKey);
    		Wsdl wsdl = new Wsdl(new URI("file://"+wsdlLocation).toURL().toString());
    		WsdlDetail wsdlDetail = new WsdlDetail(wsdl,wsdlMapping);
    		Publish_wsdl publish = new Publish_wsdl(wsdlDetail, authToken.getAuthInfo());
    		WsdlDetail wsdlDetail2 = publishing.publish_wsdl(publish);
    		printWsdlDetail(wsdlDetail2);
	    	System.out.println("Done.");
		} catch (InvalidParameterException e1) {
			e1.printStackTrace();
		} catch (UDDIException e2) {
			e2.printStackTrace();
		} catch (URISyntaxException e3) {
			e3.printStackTrace();
		} catch (MalformedURLException e4) {
			e4.printStackTrace();
		}
    }
    
	public static void printWsdlDetail(WsdlDetail wsdlDetail) {
		System.out.println();
		System.out.println(wsdlDetail.toXML());
	}

    public static void main(String[] args) throws Exception {
        WSDL2UDDIPublishing demo = new WSDL2UDDIPublishing();
        demo.publishWSDL2Registry(args[0]);
    }
}
